package com.zy.kotlinmvplib.ui.home

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.zy.kotlinbaselibrary.base.activity.BaseActivity
import com.zy.kotlinmvplib.R
import com.zy.kotlinmvplib.base.CommonViewPagerAdapter
import com.zy.kotlinmvplib.entity.TabBean
import com.zy.kotlinmvplib.ui.home.fragment1.Fragment1
import com.zy.kotlinmvplib.ui.home.fragment2.Fragment2
import com.zy.kotlinmvplib.ui.home.fragment3.Fragment3
import kotlinx.android.synthetic.main.act_home.*

class HomeActivity: BaseActivity() {

    private val mTitles = arrayOf("首页", "发现", "推荐")

    private val viewpagerAdapter: CommonViewPagerAdapter by lazy {
        CommonViewPagerAdapter(supportFragmentManager, this, mFragments, mTitles)
    }

    private val mFragments: ArrayList<Fragment> by lazy {
        ArrayList<Fragment>()
    }
    private val mTabEntities: ArrayList<CustomTabEntity> by lazy {
        ArrayList<CustomTabEntity>()
    }

    private val mIconUnselectIds = intArrayOf(
        R.mipmap.ic_launcher,
        R.mipmap.ic_launcher,
        R.mipmap.ic_launcher
    )
    private val mIconSelectIds = intArrayOf(
        R.mipmap.tab_contact_select,
        R.mipmap.tab_contact_select,
        R.mipmap.tab_contact_select
    )

    override fun getLayoutId(savedInstanceState: Bundle?): Int {
        return R.layout.act_home
    }

    override fun initData() {

        mFragments.add(Fragment1.newInstance())
        mFragments.add(Fragment2.newInstance())
        mFragments.add(Fragment3.newInstance())



        for((position, value) in mTitles.withIndex()){
            mTabEntities.add(TabBean(value,  mIconSelectIds[position], mIconUnselectIds[position]))
        }

        act_homeTabLayout.setTabData(mTabEntities)

        act_homeTabLayout.showMsg(1, 10)

        act_homeViewPager.adapter = viewpagerAdapter
        act_homeViewPager.offscreenPageLimit = 3
    }

    override fun initEvent() {

        act_homeTabLayout.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                act_homeViewPager.currentItem = position
            }

            override fun onTabReselect(position: Int) {
            }

        })

        act_homeViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(p0: Int) {
            }

            override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {

            }

            override fun onPageSelected(p0: Int) {
                act_homeTabLayout.currentTab = p0
            }



        })
    }
}